/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.module.properties;

import java.util.HashMap;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.parsing.Tokenizer;

/**
 *
 * @author Main
 */
public class KeyValueParser implements PropertyParser {
    private PropertyParser keyParser;
    private PropertyParser valueParser;

    public void setAttribures(HashMap<String, String> attributes, NodeList childElements) throws IllegalArgumentException {
        //should have element.parser-name for each parser and its attributes in a x=a;...;n=m string
        for (int i=0; i<childElements.getLength(); i++) {
            Node parser = childElements.item(i);
            String parserName = parser.getNodeName();
            if (!parserName.equals("#text")) {
                HashMap<String,String> parserAttributes = new HashMap<String,String>();
                NamedNodeMap attrMap = parser.getAttributes();
                if (attrMap!=null && attrMap.getLength()!=0) {
                    for (int z=0; z<attrMap.getLength(); z++) {
                        parserAttributes.put(attrMap.item(z).getNodeName().trim(), attrMap.item(z).getNodeValue().trim());
                    }
                }
                try {
                    if (parserName.startsWith("key.")) { //is a key parser
                        keyParser = PropertyParserFactory.createParser(parserName.substring(4), parserAttributes, parser.getChildNodes());
                    } else if (parserName.startsWith("value.")) { //is a value parser
                        valueParser = PropertyParserFactory.createParser(parserName.substring(6), parserAttributes, parser.getChildNodes());
                    } else
                        throw new IllegalArgumentException("Illegal keyvalue-parser element name: " + parserName);
                } catch (IllegalArgumentException ex) {
                    throw new IllegalArgumentException("at parser " + parserName + ", " + ex.getMessage());
                }
            }
        }
        if (keyParser==null) 
            throw new IllegalArgumentException("Missing key parser element");
        if (valueParser==null)
            throw new IllegalArgumentException("Missing value parser element");
    }

    public Object parseProperty(String arg) throws IllegalArgumentException {
        // returns an array of Objects
        String[] splitArgs = new Tokenizer(arg, ':').getTokens();
        Object key = keyParser.parseProperty(splitArgs[0]);
        Object value = valueParser.parseProperty(splitArgs[1]);
        return new Object[] { key, value };
    }

    public String getDescription() {
        return "Key Value pair (" + keyParser.getPropertyType() + ": " + valueParser.getPropertyType();
    }
    
    public String getPropertyType() {
        return keyParser.getPropertyType() + ": " + valueParser.getPropertyType();
    }

    public void setKeyParser(PropertyParser keyParser) {
        this.keyParser = keyParser;
    } public PropertyParser getKeyParser() { return keyParser; }

    public void setValueParser(PropertyParser valueParser) {
        this.valueParser = valueParser;
    } public PropertyParser getValueParser() { return valueParser; }

    public String propToString(Object property, ChainNode context) {
        Object keyObject = ((Object[])property)[0];
        Object valueObject = ((Object[])property)[1];
        return keyParser.propToString(keyObject, context) + ": " + valueParser.propToString(valueObject, context);
    }
}
